package com.example.leetcode.trainingcamp.week3.Sunday;

import java.util.Stack;

/**
 * 给你一个字符串表达式 s ，请你实现一个基本计算器来计算并返回它的值。
 * s 包含 （），数字和 操作符 + - * /
 */
public class Offer16_26II {

    public int calculate(String s){
        int len = s.length();
        int i =0 ;
        Stack<Integer> nums = new Stack<>();
        Stack<Character> ops = new Stack<>();
        while (i<len){
            char c = s.charAt(i);
            if (c == ' '){
                i++;
                continue;
            }
            if (isNumber2(c)){
                int num = c - '0';
                for (i = i+1;i<len;i++){
                    char c1 = s.charAt(i);
                    if (isNumber2(c1)){
                        num = num * 10 + c1 - '0';
                    }else {
                        break;
                    }
                }
                nums.push(num);
                continue;
            }
            if(ops.isEmpty() || c == '('){
//                ops.push(c);
//                i++;
                if ( c == '-' && nums.isEmpty()){
                    // 第一个数字是负数
                    nums.push(0);
                }
                ops.push(c);
                i++;
            }else if (c != ')'){
                while (!ops.isEmpty()){
                    Character peek = ops.peek();
                    boolean b = priority2(peek, c);
                    if (b){
                        cal(nums,ops);
                    }else {
                        ops.push(c);
                        i++;
                        break;
                    }
                }
            }else {
                while (ops.peek() != '('){
                    cal(nums,ops);
                }
                ops.pop();
                i++;
            }
        }
        while (!ops.isEmpty()){
            cal(nums,ops);
        }
        return nums.pop();
    }

    public boolean priority2(char c1,char c2){
        if (c1 == '*' || c1 == '/'){
            return true;
        }
        if ((c1 == '+' || c1 == '-') && (c2 == '+' || c2 == '-') ){
            return true;
        }
        return false;
    }

    public void cal(Stack<Integer> nums,Stack<Character> ops){
        Integer nums2 = nums.pop();
        Integer nums1 = nums.pop();
        Character c = ops.pop();
        int i = cal2(nums1, nums2, c);
        nums.push(i);
    }

    public int cal2(int nums1,int nums2,char c){
        int result;
        switch (c){
            case '+':
                result = nums1 + nums2;
                break;
            case '-':
                result = nums1 - nums2;
                break;
            case '*':
                result = nums1 * nums2;
                break;
            case '/':
                result = nums1 /nums2;
                break;
            default:
                throw new IllegalStateException("Unexpected value: " + c);
        }
        return result;
    }

    public boolean isNumber2(Character c){
        return c>='0' && c<= '9';
    }
}

class TestOffer16_26II{
  public static void main(String[] args) {
    Offer16_26II o = new Offer16_26II();
    String s = "-2+1";
    System.out.println(o.calculate(s));
  }
}